package cn.ex.ymcc.web.controller;

import cn.ex.ymcc.domain.PayOrder;
import cn.ex.ymcc.dto.AlipayNotifyDto;
import cn.ex.ymcc.dto.PayApplyDto;
import cn.ex.ymcc.result.JSONResult;
import cn.ex.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 支付接口
 */
@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;


    /**
     * 根据订单号校验支付单
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        // 支付单已经存在那么就返回success  支付单没有存在就返回false
        return payOrder == null? JSONResult.error() : JSONResult.success();
    }

    /**
     * 发起支付
     * @param dto
     * @return
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        String formStr= payOrderService.apply(dto);// 向支付宝发起申请  返回from表单
        return JSONResult.success(formStr);
    }


    /**
     * 支付宝的异步回调接口
     * @param dto
     * @return
     */
    @RequestMapping("/alipay/notify")
    public String notify(AlipayNotifyDto dto){
        try {
            return payOrderService.notify(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return "Fail";
        }
    }



}